#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
    pid_t id = fork();
      
    if(id == 0) 
    {
        int cnt = 2;
        while(cnt)
        {
            printf("child is running, id:%d, ppid:%d\n", getpid(), getppid());
            sleep(1);
            cnt--;
        }
        exit(2); //子进程退出
    }
  
    int status = 0; //存储子进程退出状态
    pid_t rid = waitpid(id, &status, 0);
//    if(rid > 0) //等待成功
//        printf("wait success, status:%d, exit code:%d, exit sign:%d\n", status, (status>>8)&0xff, status&0x7f); //位操作
    
    if(rid > 0) //等待成功
    {
        if(WIFEXITED(status)) //子进程正常退出
            printf("wait success, status:%d, exit code:%d\n", status, WEXITSTATUS(status)); //提取退出码 宏
        else  //子进程异常退出
            printf("child process error!\n");
    }

    return 0;
}
